package io.modifier.basic.compose.hooks.data

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import io.modifier.basic.compose.hooks.comm.SetFalseFn
import io.modifier.basic.compose.hooks.comm.SetTrueFn
import io.modifier.basic.compose.hooks.comm.SetValueFn
import io.modifier.basic.compose.hooks.comm.ToggleFn
import io.modifier.basic.compose.hooks.comm.invoke

/*
  Description: A hook to conveniently manage Boolean state
*/

@Composable
fun useBoolean(default: Boolean = false): BooleanHolder {
    val (state, setState, getState) = useGetState(default)
    return remember {
        BooleanHolder(
            state = state, // boolean state
            toggle = { setState.invoke(!getState()) }, // toggle fun
            setValue = { b: Boolean -> setState.invoke(b) }, // set fun
            setTrue = { setState.invoke(true) }, // setTrue
            setFalse = { setState.invoke(false) } // setFalse
        )
    }
}

@Stable
data class BooleanHolder(
    val state: State<Boolean>,
    val toggle: ToggleFn,
    val setValue: SetValueFn<Boolean>,
    val setTrue: SetTrueFn,
    val setFalse: SetFalseFn,
)
